﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Homework1602
{
    internal class Program
    {
        const string FILENAME = "D:\\CSharpHomework\\csharp2024\\Homework1602\\Data.txt";
        const int ArrayCount = 10000;
        /// <summary>
        /// 1.用C#语言编写如下代码，生成一组数据，以.txt格式存到制定文件路径中。格式为“编号，姓名（三个英文字母，首字母大写，不能重复），年龄，生日（一定要是合法生日），身高，性别”，要求年龄在0-100之间，产生10000个。
        /// 2.把上面生成的数据以男女分组，分别统计男女的平均年龄、平均身高
        /// </summary>

        static void Main(string[] args)
        {
            // 检查文件是否存在并删除
            if (File.Exists(FILENAME))
            {
                File.Delete(FILENAME);
            }

            var random = new Random();
            var usedNames = new HashSet<string>();
            var data = new List<string>();

            // 生成姓名，三个字母，首字母大写，不重复
            var GenerateName = () =>
            {
                string name;
                do
                {
                    name = ((char)random.Next('A', 'Z') + "") + ((char)random.Next('a', 'z')) + "" + ((char)random.Next('a', 'z'));
                } while (usedNames.Contains(name));
                usedNames.Add(name);
                return name;
            };

            // 生成合法的生日
            var GenerateBirthDate = () =>
            {
                int year = random.Next(1950, 2010);
                int month = random.Next(1, 13);
                int day = random.Next(1, DateTime.DaysInMonth(year, month));
                return new DateTime(year, month, day);
            };

            for (int i = 0; i < ArrayCount; i++)
            {
                var name = GenerateName();
                var birthDate = GenerateBirthDate();
                var age = DateTime.Today.Year - birthDate.Year;
                if (birthDate > DateTime.Today.AddYears(-age)) age--;
                var height = random.Next(150, 201);
                var gender = random.Next(0, 2) == 0 ? "Male" : "Female";

                // 构建数据行
                var line = $"{i + 1},{name},{age},{birthDate.ToShortDateString()},{height},{gender}";
                data.Add(line);
            }

            // 写入文件
            File.WriteAllLines(FILENAME, data);
            Console.WriteLine("已成功写入");

            // 使用ReadAllLines读取集合
            var lines = File.ReadAllLines(FILENAME);
            var parts = lines.Select(line => line.Split(','));
            Console.WriteLine("读取成功！");

            // 使用LINQ分组并计算平均年龄和身高
            var groupedByGender = parts.GroupBy(p => p[5])
                .Select(group => new
                {
                    Gender = group.Key,
                    AverageAge = group.Average(p => DateTime.Today.Year - DateTime.Parse(p[3]).Year),
                    AverageHeight = group.Average(p => int.Parse(p[4]))
                })
                .ToList();

            // 输出结果
            foreach (var group in groupedByGender)
            {
                Console.WriteLine($"Gender: {group.Gender}, Average Age: {group.AverageAge:F2}, Average Height: {group.AverageHeight:F2}");
            }
        }
    }
}
